<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include "securities.php";
class Manage_slide_promotions extends Securities {

	function __construct() {
		parent::__construct('manage_slide_promotions');
		$this -> form_validation -> set_error_delimiters('<span class="error">', '</span>');
	}
	
	//Function use to list all slide promotion of home page website
	public function index()
	{
		$data['title']="Manage slide promotion";
		$data['manage_datatable'] = get_slide_promotion_manage_table($this -> mod_globals -> select_all(PREFIX_TABLE . 'slide_promotions'), $this -> uri -> segment(1));
		$this->load->view(B_MASTER.'index',$data);
	}
	//Function use to update slide information and image slide
	public function update_slide($slide_id=0){
		
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}
		$data['title'] = "Update slide promotion";
		
		$this -> form_validation -> set_rules('title', 'Title', 'required');
		
		if ($this -> form_validation -> run() === FALSE) {
			$data['slide']=$this->mod_globals->select_where(PREFIX_TABLE."slide_promotions",array("slide_promotion_id"=>$slide_id));
			$this -> load -> view(B_MASTER . 'index', $data);
		}else{
			
			$img="default.jpg";
			$data_update_slide = array('title' => $this -> input -> post('title',TRUE), 'description' => $this -> input -> post('description',TRUE), 'img_name' => $img);

			$is_saved = $this -> mod_globals -> update(PREFIX_TABLE . "slide_promotions", $data_update_slide,array("slide_promotion_id"=>$slide_id));
			if ($is_saved) {
				$this -> session -> set_flashdata('success', 'New slide was updated success.');
				redirect('manage-slide-promotions');
			} else {
				$this -> session -> set_flashdata('error', 'New slide was updated fail.');
				redirect('manage-slide-promotions');
			}
		}
	}
	
	//Function used to create new slide image promotion
	public function insert_slide(){
		
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}
		$data['title'] = "Create slide promotion";
		
		$this -> form_validation -> set_rules('title', 'Title', 'required');
		
		if ($this -> form_validation -> run() === FALSE) {
			//$data['slide']=$this->mod_globals->select_where(PREFIX_TABLE."slide_promotions",array("slide_promotion_id"=>$slide_id));
			$this -> load -> view(B_MASTER . 'index', $data);
		}else{
			
			$img="default.jpg";
			$data_update_slide = array('title' => $this -> input -> post('title',TRUE), 'description' => $this -> input -> post('description',TRUE), 'img_name' => $img);

			$is_saved = $this -> mod_globals -> insert(PREFIX_TABLE . "slide_promotions", $data_update_slide);
			if ($is_saved) {
				$this -> session -> set_flashdata('success', 'New slide was created success.');
				redirect('manage-slide-promotions');
			} else {
				$this -> session -> set_flashdata('error', 'New slide was created fail.');
				redirect('manage-slide-promotions');
			}
		}
	}
	
	//Function used to delete slide promotion from our website
	public function delete_slide($slide_id=0){
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$whe_delete_slide = array('slide_promotion_id' => $slide_id);
		$is_deleted = $this -> mod_globals -> delete(PREFIX_TABLE . 'slide_promotions', $whe_delete_slide);
		if ($is_deleted) {
			$this -> session -> set_flashdata('success', 'Slide data was deleted success.');
			redirect('manage-slide-promotions');
		} else {
			$this -> session -> set_flashdata('error', 'Slide owner data was deleted fail.');
			redirect('manage-slide-promotions');
		}
	}
}